#ifndef DEMUXTHREAD_H
#define DEMUXTHREAD_H

#include <QObject>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QDateTime>
#include "ffinclude.h"

class VideoPlayer;

/*
    解复用线程
*/
class DemuxThread : public QThread
{
    Q_OBJECT
public:
    explicit DemuxThread(VideoPlayer* player);
    ~DemuxThread();

    void play();
    void pause();
    void stop();
    void seek(int ms);

signals:
    void inited();
    void eof();
    void seekDone(bool success,const QString& errMsg);
    void error(const QString& errMsg);

protected:
    void run();

private:
    bool openStream();
    void closeStream();
    void startPacketQueue();
    void stopPacketQueue();

    bool isQueueFull(AVStream* stream,PacketQueue* queue);
    bool isLiveStreamUrl(const QString& url);
    bool enqueueVideoAttachFrame();

    static int demuxInterruptCallback(void *ctx);

private:
    VideoPlayer* m_player;

    QMutex m_waitMutex;
    QWaitCondition m_waitCond;

    bool m_reqStop;
    bool m_reqPause;
    bool m_reqSeek;
    qint64 m_seekPos;

    bool m_needQueueAttachFrame;
    bool m_startPosSeekDone;
    bool m_lastReqPause;
    QString m_lastErrorMsg;
    QDateTime m_streamOpenDateTime;
    QDateTime m_lastReadDateTime;
};

#endif // DEMUXTHREAD_H
